﻿using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIP.Data;
using VIP.Data.EFProvider;
using VIP.Data.Entities;
using VIP.Domain.Core.DomainObject;
using VIP.Infrastructure.Caching;

namespace VIP.Domain.Core.Factories
{
    public class OrderLoader : AbstractAggregateRootLoader<OrderObj>, IAggregateRootLoader<OrderObj>
    {
        private IUnitOfWork<VIPDbContext> _unitOfWork;

        public OrderLoader(IUnitOfWork<VIPDbContext> unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public override string AggregateRootCachePrefix => throw new NotImplementedException();


        public async override Task<OrderObj> LoadFromDbAsync(Guid id)
        {
            var orderRep = _unitOfWork.GetRepositoryAsync<Order>();
            var orderItemRep = _unitOfWork.GetRepositoryAsync<OrderItem>();
            var obj = new OrderObj() { };
            obj.Order = await orderRep.SingleAsync(p => p.Id == id);

            Func<IQueryable<OrderItem>, IOrderedQueryable<OrderItem>> orderbyExp = ob => ob.OrderBy(e => e.OrderIndex);
            var items = await orderItemRep.GetListAsync(p => p.MId == id, orderBy: orderbyExp, size: int.MaxValue);
            obj.Items.OrderItemEntities = items.Items;
            return obj;
        }
    }
}
